/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class CheckoutModel extends AbstractModel
{
	protected List<OrderTotalModel> totals;
	protected List<ShippingModel> shippings;
	protected List<PaymentModel> payments;
	
	protected String selectedShippingModule = "";
	protected String selectedShippingService = "";
	protected boolean shippingUseCheapest = false;
	protected String selectedPaymentModule = "";
	protected String actionCompleted = "";
	protected int errorCode = 0;
	protected String errorMessage = "";
	protected String paymentErrorMessage = "";

	
	public CheckoutModel()
	{
		super();
		totals = new ArrayList<OrderTotalModel>();
		shippings = new ArrayList<ShippingModel>();
		payments = new ArrayList<PaymentModel>();
	}

	public void update(JSONObject jsonObject)
	{
		selectedShippingModule = convertToString(jsonObject,"shipping_module");
		selectedShippingService = convertToString(jsonObject,"shipping_service");
		shippingUseCheapest = convertToBoolean(jsonObject, "shipping_use_cheapest");
		selectedPaymentModule = convertToString(jsonObject,"payment_module");
		actionCompleted = convertToString(jsonObject,"action_completed");
		errorCode = convertToInteger(jsonObject,"error_code");
		errorMessage = convertToString(jsonObject,"error_message");
		paymentErrorMessage = convertToString(jsonObject,"payment_error_message");
		totals = new ArrayList<OrderTotalModel>();
		shippings = new ArrayList<ShippingModel>();
		payments = new ArrayList<PaymentModel>();
		buildTotals(jsonObject);
		buildShippings(jsonObject);
		buildPayments(jsonObject);
	}

	protected void buildShippings(JSONObject jsonObject)
	{
		shippings.clear();

		try {
			JSONValue shippingValue = jsonObject.get("shipping");

			if (shippingValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = shippingValue.isArray()) != null)
				{

					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonSObject;
						if ((jsonSObject = jsonValue.isObject()) != null)
						{
							ShippingModel shippingModel = new ShippingModel(jsonSObject);
							shippings.add(shippingModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	protected void buildPayments(JSONObject jsonObject)
	{
		payments.clear();

		try {
			JSONValue paymentValue = jsonObject.get("payment");

			if (paymentValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = paymentValue.isArray()) != null)
				{
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonSObject;
						if ((jsonSObject = jsonValue.isObject()) != null)
						{
							PaymentModel paymentModel = new PaymentModel(jsonSObject);
							payments.add(paymentModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}
	
	protected void buildTotals(JSONObject jsonObject)
	{
		totals.clear();

		try {
			JSONValue totalValue = jsonObject.get("totals");

			if (totalValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = totalValue.isArray()) != null)
				{

					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonOObject;
						if ((jsonOObject = jsonValue.isObject()) != null)
						{
							OrderTotalModel otModel = new OrderTotalModel(jsonOObject);
							totals.add(otModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	public List<OrderTotalModel> getTotals() {
		return totals;
	}

	public List<ShippingModel> getShippings() {
		return shippings;
	}

	public String getSelectedShippingModule() {
		return selectedShippingModule;
	}

	public void setSelectedShippingModule(String selectedShippingModule) {
		this.selectedShippingModule = selectedShippingModule;
	}

	public String getSelectedShippingService() {
		return selectedShippingService;
	}

	public void setSelectedShippingService(String selectedShippingService) {
		this.selectedShippingService = selectedShippingService;
	}

	public List<PaymentModel> getPayments() {
		return payments;
	}

	public String getSelectedPaymentModule() {
		return selectedPaymentModule;
	}

	public void setSelectedPaymentModule(String selectedPaymentModule) {
		this.selectedPaymentModule = selectedPaymentModule;
	}

	public String getActionCompleted() {
		return actionCompleted;
	}

	public void setActionCompleted(String actionCompleted) {
		this.actionCompleted = actionCompleted;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getPaymentErrorMessage() {
		return paymentErrorMessage;
	}

	public boolean isShippingUseCheapest() {
		return shippingUseCheapest;
	}
}
